package lab4;

public class SortIntArray {

	public static void bSort (int[] a)
	{
		for (int j = 1; j <= a.length; j++)
			for (int i = 0; i<a.length-j; i++)
				if (a[i]>a[i+1])
				{
					int t=a[i];
					a[i]=a[i+1];
					a[i+1]=t;
				}
	}
	
	public static void bSort (int[] a, int left, int right)
	{
		for (int j = left; j <= right; j++)
			for (int i = left; i<=right-j; i++)
				if (a[i]>a[i+1])
				{
					int t=a[i];
					a[i]=a[i+1];
					a[i+1]=t;
				}
	}
	
	public static void sSort (int[] a)
	{
		for (int j = 0; j < (a.length-1)/2; j++)
		{
			int maxIndex=j;
			int max = a[j];
			int min = a[j];
			int minIndex=j;
			for (int i = j; i<a.length-j; i++)
			{
				if (a[i]>max)
				{
					max=a[i];
					maxIndex=i;
				}
				if (a[i]<min)
				{
					min=a[i];
					minIndex=i;
				}
			}
			a[maxIndex]=a[a.length-1-j];
			a[a.length-1-j]=max;
			a[minIndex]=a[j];
			a[j]=min;
		}
	}
	
	public static void sSort (int[] a, int left, int right)
	{
		for (int j = left; j < (left+right)/2; j++)
		{
			int maxIndex=j;
			int max = a[j];
			int min = a[j];
			int minIndex=j;
			for (int i = left; i<=right-j; i++)
			{
				if (a[i]>max)
				{
					max=a[i];
					maxIndex=i;
				}
				if (a[i]<min)
				{
					min=a[i];
					minIndex=i;
				}
			}
			a[maxIndex]=a[right-j];
			a[right-j]=max;
			a[minIndex]=a[left+j];
			a[left+j]=min;
		}
	}
	
	public static void main(String[] args) {
		int[] a = {-19, 19, 20, 5, 10, 16, 0, -4, 7, 4, -3, 9, 0, 53, 22, 900, -4214, 21, 0, -19};
		sSort(a, 1, 6);
		for (int i = 0; i<a.length; i++) System.out.print(a[i]+" ");
	}

}
